//
//  EpointTodoAttachListViewController.m
//  EpointFrame5
//
//  Created by wangzhigang on 14-2-14.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointTodoAttachListViewController.h"
#import "MailMainTableViewCell.h"
#import "EpointAttachOpenViewController.h"
#import "UIView+Addtions.h"
#import "MaterialModel.h"

@interface EpointTodoAttachListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation EpointTodoAttachListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"附件列表";
    
    self.dataArray = [NSMutableArray arrayWithCapacity:10];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, AppWidth, AppHeight-SystemTakeHeight)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self getData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData {
    [_dataArray addObjectsFromArray:[initParam objectForKey:@"attachArray"]];
    [_tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    MailMainTableViewCell *cell = (MailMainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MailMainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    MaterialModel *singleFile = [_dataArray objectAtIndex:indexPath.row];
    NSString *filename = singleFile.MaterialInstanceName;
    if ([filename hasSuffix:@".doc"]||[filename hasSuffix:@".docx"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"img_attach_doc"];
    } else if([filename hasSuffix:@"xls"]||[filename hasSuffix:@".xlsx"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"img_attach_excel"];
    } else if ([filename hasSuffix:@"ppt"]||[filename hasSuffix:@".pptx"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"img_attach_ppt"];
    } else if([filename hasSuffix:@"pdf"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"img_pdf_48"];
    } else if([filename hasSuffix:@"txt"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"img_txt_48"];
    } else if([filename hasSuffix:@"jpg"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"img_jpg_48"];
    } else {
        cell.leftImageView.image = [UIImage imageNamed:@"img_attach_file"];
    }
    cell.leftImageView.size = CGSizeMake(30, 30);
    cell.titleLabel.text = filename;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    MaterialModel *singleFile = [_dataArray objectAtIndex:indexPath.row];
    if ([singleFile.isBigFile boolValue]) {
        [self toastMessage:BigFileCantBeDownloaded];
        return;
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:singleFile.DownLoadUrl forKey:@"fileurl"];
    [params setObject:singleFile.MaterialInstanceName forKey:@"title"];
    [params setObject:singleFile.MaterialInstanceGuid forKey:@"folderguid"];
    EpointAttachOpenViewController *ao = [[EpointAttachOpenViewController alloc]init];
    ao.passParam = params;
    [self.navigationController pushViewController:ao animated:YES];
}

@end
